//
//  AlarmStatisticsArmView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/6/15.
//

import Foundation
import UIKit

// 撤布防Tab
class AlarmStatisticsArmView: UIView, PullToRefreshPresentable {
    // 各网点统计
    let netSumV = AlarmNetSummaryView()
    
    let unarmedTable = UnarmedTableView()
        
    var model: AlarmCbfqyCountModal? = AlarmCbfqyCountModal(dictLabel: "营业网点", dictValue: "1") {
        didSet {
            unarmedTable.areaType = model
            fetchList()
        }
    }
    
    var models: [AlarmCbfqyCountModal?]? {
        didSet {
            self.netSumV.updateUI(withModals: models, cur: model)
        }
    }
    
    var deptId: String?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        setupPullToRefresh(on: unarmedTable.tableView, bottomRefreshing: false)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        unarmedTable.tableView.removeAllPullToRefresh()
    }
    
    func reloadAction() {
        fetchList()
    }
    
    func reloadData(withParam param: AlarmCommonParam) {
        self.deptId = param.tbCompanyId
        
        // 区域类型统计-撤布防
        API.getAlarmCbfqyCount(withParam: param) { responseModel in
            DispatchQueue.main.async {
                self.models = responseModel.models
            }
        }
        
        fetchList()
    }
    
    func fetchList() {
        unarmedTable.datas = []
        showToastActivity()
        API.getAlarmCbfdepts(withParam: AlarmWBFParam(deptId: deptId, abfType: model?.dictValue)) { responseModel in
            DispatchQueue.main.async {
                self.unarmedTable.datas = responseModel.model?.list ?? []
                self.hideToastActivity()
            }
        }
    }
    
    func setupUI() {
        netSumV.handleNetSummaryWith = { [self] (m) -> () in
            self.model = m
            self.netSumV.updateUI(withModals: self.models, cur: m)
            self.fetchList()
        }
        addSubview(netSumV)
        netSumV.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(140)
        }
        
        unarmedTable.areaType = model
        unarmedTable.layer.cornerRadius = 10
        unarmedTable.layer.masksToBounds = true
        addSubview(unarmedTable)
        unarmedTable.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview().offset(-10)
            make.top.equalTo(netSumV.snp.bottom).offset(10)
        }
    }
}

class AlarmNetSummaryView: UIView {
    
    open var handleNetSummaryWith:((_ m: AlarmCbfqyCountModal?) -> ())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        updateUI(withModals: [
            AlarmCbfqyCountModal(dictLabel: "营业网点", dictValue: "1"),
            AlarmCbfqyCountModal(dictLabel: "ATM", dictValue: "2"),
            AlarmCbfqyCountModal(dictLabel: "离行自助", dictValue: "3"),
            AlarmCbfqyCountModal(dictLabel: "办公区域", dictValue: "4"),
            AlarmCbfqyCountModal(dictLabel: "金库", dictValue: "5"),
        ], cur: AlarmCbfqyCountModal(dictLabel: "营业网点", dictValue: "1"))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func updateUI(withModals modals: [AlarmCbfqyCountModal?]?, cur: AlarmCbfqyCountModal?) {
        removeAllSubViews()
        
        let width = (ScreenWidth - 20) / 5.0
        var last: AlarmNetCardView?
        
        if let mds = modals {
            for (index, modal) in mds.enumerated() {
                let v = AlarmNetCardView()
                v.updateUI(withModal: modal, isCur: cur?.dictValue == modal?.dictValue)
                v.handleNetSummaryWith = handleNetSummaryWith
                addSubview(v)
                v.snp.makeConstraints { make in
                    make.width.equalTo(width)
                    make.top.bottom.equalToSuperview()
                    if last == nil {
                        make.left.equalToSuperview().offset(10)
                    } else {
                        make.left.equalTo(last!.snp.right)
                    }
                    
                    if index == mds.count - 1 {
                        make.right.equalToSuperview()
                    }
                }
                last = v
            }
        }
    }
    
    func setupUI() {
        backgroundColor = .white
        layer.cornerRadius = 10
        layer.masksToBounds = true
    }
}

class AlarmNetCardView: UIControl {
    let iconIV = UIImageView()
    let titleL = UILabel()
    let tipsL = UILabel()
    let numL = UILabel()
    let lineV = UIView()

    var model: AlarmCbfqyCountModal?
    
    open var handleNetSummaryWith:((_ m: AlarmCbfqyCountModal?) -> ())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        addTarget(self, action: #selector(handleNetSummaryTapped), for: .touchUpInside)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var isHighlighted: Bool {
        didSet {
            if isHighlighted {
                layer.opacity = 0.6
            } else {
                layer.opacity = 1.0
            }
        }
    }

    @objc func handleNetSummaryTapped() {
        handleNetSummaryWith?(model)
    }
    
    func updateUI(withModal modal: AlarmCbfqyCountModal?, isCur: Bool?) {
        self.model = modal
        
        titleL.text = modal?.dictLabel
        numL.text = "\(modal?.bfTotal ?? 0)/\(modal?.total ?? 0)"

        switch UnarmedTodayType(rawValue: modal?.dictValue ?? "") {
        case .WorkNet:
            iconIV.image = UIImage(named: "ic_alarmnet_1")
        case .ATMs:
            iconIV.image = UIImage(named: "ic_alarmnet_2")
        case .SelfHelp:
            iconIV.image = UIImage(named: "ic_alarmnet_3")
        case .OfficeArea:
            iconIV.image = UIImage(named: "ic_alarmnet_4")
        case .Treasury:
            iconIV.image = UIImage(named: "ic_alarmnet_5")
        default:  break
        }
        
        lineV.isHidden = !(isCur == true)
    }

    func setupUI() {
        iconIV.contentMode = .scaleAspectFit
        addSubview(iconIV)
        iconIV.snp.makeConstraints { make in
            make.width.height.equalTo(32)
            make.top.equalToSuperview().offset(15)
            make.centerX.equalToSuperview()
        }
        
        titleL.adjustsFontSizeToFitWidth = true
        titleL.textAlignment = .center
        titleL.textColor = .hex("#333333")
        titleL.font = .systemFont(ofSize: 15)
        addSubview(titleL)
        titleL.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(2)
            make.right.equalToSuperview().offset(-2)
            make.top.equalTo(iconIV.snp.bottom).offset(9)
        }
        
        tipsL.text = "布/总"
        tipsL.textAlignment = .center
        tipsL.textColor = .hex("#666666")
        tipsL.font = .systemFont(ofSize: 13)
        addSubview(tipsL)
        tipsL.snp.makeConstraints { make in
            make.top.equalTo(titleL.snp.bottom).offset(8)
            make.left.equalTo(titleL.snp.left)
            make.right.equalTo(titleL.snp.right)
        }

        numL.adjustsFontSizeToFitWidth = true
        numL.textAlignment = .center
        numL.textColor = .hex("#333333")
        numL.font = .systemFont(ofSize: 15)
        addSubview(numL)
        numL.snp.makeConstraints { make in
            make.top.equalTo(tipsL.snp.bottom).offset(8)
            make.left.equalTo(titleL.snp.left)
            make.right.equalTo(titleL.snp.right)
        }
        
        lineV.isHidden = true
        lineV.backgroundColor = .primary
        lineV.layer.cornerRadius = 1
        lineV.layer.masksToBounds = true
        addSubview(lineV)
        lineV.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-8)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(2)
        }
    }
}

